﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IRepository;
using Models;
using Spring.Data.Common;
using Infrastructure;
using System.Data;
using Spring.Data.Generic;
using System.Reflection;

namespace RepositoryMSSQL
{
    public class TaskRepository : ITaskRepository
    {
        private static string strBase = "select Code, Name, Start_Date, End_Date, Creator, Remark "
            + " from Task_Info ";

        public TaskRepository() { }

        public void Create(TaskInfo info)
        {
            string strSql = "insert into Task_Info(Code, Name, Start_Date, End_Date, Creator, Remark) "
                + " values(@Code, @Name, @StartDate, @EndDate, @Creator, @Remark) ";

            string strSearch = "SELECT isnull(max(Code),'T000000000') FROM Task_Info ";
            string strMaxNo = DBHelper.SpringDBTemplate.ExecuteScalar(CommandType.Text, strSearch).ToString();
            int nNum = Int32.Parse(strMaxNo.Substring(1)) + 1;
            info.Code = "T" + nNum.ToString("000000000");

            DBHelper.CreateObj(info, strSql);

            //IDbParameters para = DBHelper.SpringDBTemplate.CreateDbParameters();
            //para.Add("Code", DbType.String).Value = info.Code;
            //para.Add("Name", DbType.String).Value = info.Name;
            //para.Add("Start_Date", DbType.DateTime).Value = info.StartDate;
            //para.Add("End_Date", DbType.DateTime).Value = info.EndDate;
            //para.Add("Creator", DbType.String).Value = info.Creator;
            //para.Add("Remark", DbType.String).Value = info.Remark;

            //DBHelper.SpringDBTemplate.ExecuteScalar(CommandType.Text, strSql, para);
        }

        public void Update(TaskInfo info)
        {
            //string strSql = "update Task_Info set Name=@Name, Start_Date=@StartDate, End_Date=@EndDate, Creator=@Creator, Remark=@Remark where Code=@Code ";
        }

        internal void Delete(TaskInfo code)
        {
            throw new NotImplementedException();
        }

        public List<object> ListObj()
        {
            throw new NotImplementedException();
        }

        #region Helper Methods
        private static TaskInfo MapRow(IDataReader dr, int count)
        {
            TaskInfo obj = new TaskInfo();

            obj.Code = dr.IsDBNull(0) ? String.Empty : dr.GetString(0);
            obj.Name = dr.IsDBNull(1) ? String.Empty : dr.GetString(1);
            obj.StartDate = dr.IsDBNull(2) ? DateTime.MinValue : dr.GetDateTime(2);
            obj.EndDate = dr.IsDBNull(3) ? DateTime.MinValue : dr.GetDateTime(3);
            obj.Creator = dr.IsDBNull(4) ? String.Empty : dr.GetString(4);
            obj.Remark = dr.IsDBNull(5) ? String.Empty : dr.GetString(5);

            return obj;
        }
        #endregion

        public TaskInfo GetTaskInfo(string code)
        {
            string strSql = strBase + " where Code = @Code ";

            IDbParameters para = DBHelper.SpringDBTemplate.CreateDbParameters();
            para.Add("Code", DbType.String).Value = code;

            IList<TaskInfo> lst = DBHelper.SpringDBTemplate.QueryWithRowMapperDelegate(CommandType.Text, strSql,
                new RowMapperDelegate<TaskInfo>(MapRow), para);

            return lst.Count() > 0 ? lst[0] : null;
        }

        //internal TaskRepository() { }


        //public List<TaskInfo> ListTask(List<string> lst)
        //{
        //    //string strSql = strBase + " where Code in ( " + StringHelper.ListToInClause(lst) + ") ";

        //    IDbParameters p = DBHelper.SpringDBTemplate.CreateDbParameters();
        //    p.Add("Ids", DbType.String).Value = StringHelper.ListToXMLForID(lst);

        //    return DBHelper.SpringDBTemplate.QueryWithRowMapperDelegate(CommandType.StoredProcedure, "sp_List_Task_Info",
        //       new RowMapperDelegate<TaskInfo>(MapRow), p).ToList();

        //    //return DBHelper.SpringDBTemplate.QueryWithRowMapperDelegate(CommandType.Text, strSql,
        //    //   new RowMapperDelegate<TaskInfo>(MapRow)).ToList();
        //}

    }
}
